package com.linln.admin.system.controller;

import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.statistics.StatisticsGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存监控控制器
 * @author 测试
 * @date 2025/11/04
 */
@RestController
@RequestMapping("/cache")
public class CacheMonitorController {

    @Autowired
    private CacheManager cacheManager;

    /**
     * 获取所有缓存统计信息
     */
    @GetMapping("/stats")
    public ResultVo getAllCacheStats() {
        Map<String, Object> result = new HashMap<>();
        
        String[] cacheNames = cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            Cache cache = cacheManager.getCache(cacheName);
            result.put(cacheName, getCacheInfo(cache));
        }
        
        return ResultVoUtil.success("缓存统计信息", result);
    }

    /**
     * 获取指定缓存的统计信息
     */
    @GetMapping("/stats/{cacheName}")
    public ResultVo getCacheStats(@PathVariable String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            return ResultVoUtil.error("缓存不存在: " + cacheName);
        }
        
        return ResultVoUtil.success("缓存统计信息", getCacheInfo(cache));
    }

    /**
     * 清空指定缓存
     */
    @GetMapping("/clear/{cacheName}")
    public ResultVo clearCache(@PathVariable String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            return ResultVoUtil.error("缓存不存在: " + cacheName);
        }
        
        cache.removeAll();
        return ResultVoUtil.success("缓存已清空: " + cacheName);
    }

    /**
     * 清空所有缓存
     */
    @GetMapping("/clear/all")
    public ResultVo clearAllCache() {
        cacheManager.clearAll();
        return ResultVoUtil.success("所有缓存已清空");
    }

    /**
     * 获取缓存详细信息
     */
    private Map<String, Object> getCacheInfo(Cache cache) {
        Map<String, Object> info = new HashMap<>();
        StatisticsGateway stats = cache.getStatistics();
        
        info.put("cacheName", cache.getName());
        info.put("size", cache.getSize());
        info.put("memoryStoreSize", cache.getMemoryStoreSize());
        info.put("diskStoreSize", cache.getDiskStoreSize());
        info.put("cacheHits", stats.cacheHitCount());
        info.put("cacheMisses", stats.cacheMissCount());
        
        // 计算缓存命中率
        long hits = stats.cacheHitCount();
        long misses = stats.cacheMissCount();
        double hitRate = 0.0;
        if (hits + misses > 0) {
            hitRate = (double) hits / (hits + misses) * 100;
        }
        info.put("cacheHitPercentage", String.format("%.2f%%", hitRate));
        info.put("evictionCount", stats.cacheEvictedCount());
        
        return info;
    }
}
